package main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Main {
	
	public static final int WIDTH = 600; //Width of gaming area;
	
	public static final int HEIGHT = 600; //Height of gaming area;
	
	public static JFrame window = new JFrame("Car game!");
	public static GPanel game = new GPanel();
	public static Manager manager = new Manager(WIDTH, HEIGHT);
	public static JTextField keyInput = new JTextField();

	public static void sleep(int time) {
		try {
			Thread.sleep(time);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {

		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		game.setBounds(0, 0, WIDTH, HEIGHT);
		window.setPreferredSize(new Dimension(WIDTH + 50, HEIGHT + 50));
		game.setManager(manager);
		window.getContentPane().setLayout(null);
		{
			window.getContentPane().add(keyInput, "Center");
			keyInput.setBounds(10, 10, 0, 0);
			keyInput.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent evt) {
					manager.keys[evt.getKeyCode()] = true;
				}

				public void keyReleased(java.awt.event.KeyEvent evt) {
					manager.keys[evt.getKeyCode()] = false;
				}
			});
		}
		window.add(game);
		window.pack();
		window.setVisible(true);

		while (true) {
			game.repaint();
			game.setSize(window.getSize());
			manager.WIDTH = window.getWidth() - 40;
			manager.HEIGHT = window.getHeight() - 40;
			sleep(5);
		}
	}
}
